﻿public class Solution {
    public int MaxSubArray(int[] nums)
    {
        int result = 0;
        int max = nums[0];
        for (int i = 0; i < nums.Length; i++)
        {
            result += nums[i];
            
            if (result > max)
            {
                max = result;
            }
            if (result < 0)
            {
                result = 0;
            }
        }

        return max;
    }

    public static void Main(string[] args)
    {
        int[] nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4];
        Solution solution = new Solution();
        int result = solution.MaxSubArray(nums);
        Console.WriteLine(result);
    }
}